//
//  IweGoodsDetailCtr-cell.swift
//  eemall
//
//  Created by zhuxietong on 2018/8/31.
//  Copyright © 2018年 zhuxietong. All rights reserved.
//

import Foundation
import Eelay
import soapp
import AsyncDisplayKit


extension IweGoodsDetailCtr{
    open class BuyBar1: BuyBar {
        open override func addLayoutRules() {
            
            let line = UIView()
            contentView.eelay = [
                [sellerBt,[ee.L.Y,[12,0]],"40",44.co],
                [careBt,[sellerBt,ee.R,ee.L,4],[ee.Y],"40",44.co],
                [buyBt,[ee.T.B.R,[6,-6,-AppTheme.edge]],110.co],
                [cartBt,[ee.T.B,[6,-6]],[buyBt,ee.L,ee.R,-ThemeSet.buttonSpace],110.co],
                [line,[ee.T.L.R],"\(TP.separatorHeight)"],
            ]
            
            buyBt.bsui.set(radius: ThemeSet.buttonRadius, width: nil, color: nil)
            cartBt.bsui.set(radius: ThemeSet.buttonRadius, width: nil, color: nil)
            
            
            
            buyBt.setBackgroundImage(UIImage.gradient(colors: ThemeSet.Gradient.buy, size: [110.co,44]), for: .normal)
            
            cartBt.setBackgroundImage(UIImage.gradient(colors: ThemeSet.Gradient.cart, size: [110.co,44]), for: .normal)
            

            _ = buyBt.ui.font(size: 13).ncolor(hex: "#fff").ntitle(title: "立即购买".loc).padding(insets: [3,10]).set(radius: ThemeSet.buttonRadius, width: nil, color: nil)
//            buyBt.setBackgroundImage(UIImage(named: "shop_buy"), for: .normal)
//            sellerBt.bt.ui.padding(insets: [8,8,18,8])
//            careBt.bt.ui.padding(insets: [8,8,18,8])
            
            _ = cartBt.ui.font(size: 13).ncolor(hex: "#fff").ntitle(title: "加入购物车".loc).padding(insets: [3,10]).set(radius: ThemeSet.buttonRadius, width: nil, color: nil)
            line.backgroundColor = ._separator
        }
        open override func didLayoutRules() {
            sellerBt.bt.setImage( UIImage(named: "shop_store_gray"), for: .normal)
            
            sellerBt.bt.addTarget(self, action: #selector(seeSeller), for: .touchUpInside)
            
            careBt.bt.setImage(UIImage(named: "goods_save_n"), for: .normal)
            careBt.bt.setImage( UIImage(named:"goods_save_s")?.dynamicImage(color: ___shop.tint), for: .selected)
            careBt.bt.addTarget(self, action: #selector(careToggle), for: .touchUpInside)
            
            sellerBt.lable.ui.cl_888.font(font: UIFont.systemFont(ofSize: 9, weight: .light)).center.text("店铺".loc)
            careBt.lable.ui.cl_888.font(font: UIFont.systemFont(ofSize: 9, weight: .light)).center.text("收藏".loc)
            buyBt.addTarget(self, action: #selector(toBuy), for: .touchUpInside)
            cartBt.addTarget(self, action: #selector(addCartAction), for: .touchUpInside)
        }

    }
    
     class InfoCell1: JoCellNode {
        let tagBt = ASButtonNode()
        let nameL = JoTextNode()
        
        let priceL = ASTextNode()
        
        let postL = JoTextNode()
        let numL = JoTextNode()
        let locationL = JoTextNode()
        let preKit = PrebuyTipKit()

        var activeType = Goods.ActiveType.none
        
        override func specGetter() -> _Spec {
            
            let overTag:_Spec = .over([], over: .stack([[2,0,0,0]], childs: [
                tagBt.Spec
                ]), back: nameL.Spec)
            
            let w = (Swidth - 2.0 * AppTheme.edge)/3.0
            let bt_spec:_Spec = .stack(["h",&-.center,_justfy(.space_between),_nowrap,(Swidth-2*AppTheme.edge)], childs: [
                postL[w,_grow(1)],
                numL[w,_grow(1)],
                locationL[w,_grow(1)]
                ])
            var sep:_Spec = .stack([[16,AppTheme.edge],"v|10",_justfy(.space_between)], childs: [
                overTag,
                priceL.Spec,
                bt_spec
                ])
            switch activeType {
            case .prebuy:
                sep = .stack([[16,AppTheme.edge],"v|10",_justfy(.space_between),&-.stretch], childs: [
                    overTag,
                    priceL.Spec,
                    preKit.Spec,
                    bt_spec
                ])
            default:
                break
            }
            return sep
        }
        
        override func mo_model_reload() {
            view.backgroundColor = ._background
            
            //            separatorHeight = TP.separatorHeight
            separatorColor = UIColor._separator
            let model = DataNode.path(node: "__model", mo.model, value: NSMutableDictionary())
            activeType = Goods.ActiveType(item: model)
            switch activeType {
            case .prebuy:
                preKit.mo.model = model
            default:
                break
            }

            let price = model[float:"marketprice",0]
            let old_price = model[float:"productprice",0]
            let priceStr = "¥  ".ui.font12.color(color: ___shop.tint).astring + (price.price_display + "   ").ui.color(color: ___shop.tint).font20.astring
            
            if price < old_price
            {
                let old_priceStr = ("¥ " +  old_price.price_display).ui.color(color: ._foregroundWeak).font13.append(.strikethroughStyle, 1).append(.strokeColor, UIColor(shex:"#aaa")).astring
                priceL.attributedText = (priceStr + old_priceStr)
            }
            else{
                priceL.attributedText = priceStr
            }
            
            postL.attributedText = "\("快递".loc)：\("包邮".loc)".ui.font12.color(color: ._foreground).astring
            numL.attributedText = "\("月销量".loc)：\(model["sales","0"])\(model["unit",""])".ui.font12.color(color: ._foreground).center.astring
            
            let str = "\(model["province",""])-\(model["city",""])"
            locationL.attributedText = str.ui.font12.color(color: ._foreground).right.astring
            locationL.textNode.maximumNumberOfLines = 1
            
            let merchID = model.possible("merchid","merch.id", default: "")
            
            if merchID == "0" && Goods.self_seller_name != nil
            {
                let name = Goods.self_seller_name ?? ""
                let tagStr =  "\(name)\("自营".loc)"
                
                let tagAtr = "\(tagStr)".ui.font(font: UIFont.systemFont(ofSize: 10)).color(hex: "#fff").astring
                tagBt.setAttributedTitle(tagAtr, for: .normal)
                tagBt.backgroundColor = UIColor(shex: "#E15378")
                tagBt.contentEdgeInsets = [2,4,2,4]
                nameL.textNode.maximumNumberOfLines = 3
                nameL.attributedText = "\(tagStr)  ".ui.lineSpace(7).font12.color(color: .clear).astring + model.possible("subtitle","title", default: "").ui.font14.color(color: ._foreground).lineSpace(7).astring
            }else{
                nameL.textNode.maximumNumberOfLines = 3
                tagBt.backgroundColor = .clear
                nameL.attributedText =  model.possible("subtitle","title", default: "").ui.font14.color(color: ._foreground).lineSpace(7).astring
            }
        }
    }
    
        
}
